from typing import *


class Solution:
    def sumDigitDifferences(self, nums: List[int]) -> int:
        n = len(str(nums[0]))
        d = [[0] * 10 for _ in range(n)]
        ans = 0
        for k, num in enumerate(nums):
            for i in range(n):
                r = num % 10
                ans += k - d[i][r]
                d[i][r] += 1
                num //= 10
        return ans
